package com.mini.demos.webgame.codec;

import org.apache.mina.core.buffer.IoBuffer;

import com.mini.demos.webgame.msg.AbstractGameMsg;
import com.mini.demos.webgame.msg.LoginMsgResp;
import com.mini.demos.webgame.util.CommandConstants;
import com.mini.demos.webgame.util.Constants;
import com.mini.framework.communicate.mina.protocol.Msg;

public class LoginRespEncoder extends AbstractEncoder {

	@Override
	public void encodeBody(AbstractGameMsg msg, IoBuffer buffer) {
		LoginMsgResp loginMsgResp = (LoginMsgResp)msg;
		buffer.putInt(loginMsgResp.getCode().intValue());
		try{
			if(loginMsgResp.getCode().intValue() == 0){
				buffer.putString(loginMsgResp.getUserName(), Constants.charset.newEncoder());
				buffer.putString(CommandConstants.COMMAND_SPLIT, Constants.charset.newEncoder());
				buffer.putString(loginMsgResp.getNickName(), Constants.charset.newEncoder());
				buffer.putString(CommandConstants.COMMAND_SPLIT, Constants.charset.newEncoder());
				buffer.putString(loginMsgResp.getSex().toString(), Constants.charset.newEncoder());
				buffer.putString(CommandConstants.COMMAND_SPLIT, Constants.charset.newEncoder());
				buffer.putString(loginMsgResp.getKnowledge().toString(), Constants.charset.newEncoder());
				buffer.putString(CommandConstants.COMMAND_SPLIT, Constants.charset.newEncoder());
				buffer.putString(loginMsgResp.getMoney().toString(), Constants.charset.newEncoder());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public Class<? extends Msg> getMsgType() {
		return LoginMsgResp.class;
	}

}
